var webuploadInit = false;
var description;
$(document).ready(function() {
	initSummernote();
	// validate signup form on keyup and submit
	var validator = $("#noticeForm").validate({
		ignore: '',
		errorPlacement: function(error, element) {
			if (element.is("#marketPrice") || element.is("#memberPrice")) {
				element.parent().after(error);
			} else {
				element.after(error);
			}
		},
		rules: {
			noticeTitle: {
				required: true,
				rangelength: [1, 50]
			},
			noticeBusType: {
				required: true
			},
			noticeContent: {
				required: true,
				rangelength: [1, 200]
			},
			noticeContType: {
				required: true
			},
			autoSend: {
				required: true
			}
		},
		messages: {
			noticeTitle: '必填',
			noticeBusType: '必须选择',
			noticeContent: '必填',
			noticeContType: '必须选择',
			autoSend: '必须选择'
		}
	});

	$("#saveBtn").click(function() {
		if (IsValidated($("#tab-1")) && IsValidated($("#tab-2"))) {
			var saveBtn = $(this);
			var isFormValid = !validator.form($("#noticeForm"));
			$("#noticeForm").find('input').each(function() {
				$(this).val($.trim($(this).val()));
			});

			var descriptionText = description.getData();
			if (descriptionText.trim() == null || descriptionText.trim() == "") {
				showErrorMsg("通知图文必须填写") ;
				return;
			}
			var values = $("#noticeForm").serializeArray();
			values.push({
				name: "description",
				value: descriptionText
			});
			console.log(values)
			$.ajax({
				dataType: 'json',
				data: values,
				type: 'post',
				url: preUrl+'/notice/noticeEdit.json',
				success: function(data) {
					if (data.rspCode == '0000') {
						$('#noticeForm').trigger(data.rspMsg);
						location.href = '../notice/toNoticeList.htm';
					} else if (data.rspCode == '102') {
						showErrorMsg("系统异常,稍后重试")
					}
					saveBtn.removeClass("disabled");
				}
			});
		} else {
			showErrorMsg("数据未填写或有误")
		}
	});
});
var showErrorMsg = function(msg) {
	$('#errorMsg').text(msg).show();
}

function IsValidated(group) {
	var isValid = true;
	group.find(':input,select').each(function(i, item) {
		if (!$(item).valid()) isValid = false;
	});
	if (group.selector == "#tab-2") {

	}
	return isValid;
}
function nextTab(num) {
	var u = 0;
	var m = 0;
	var tid = "#tab-" + num;
	if (IsValidated($(tid))) {
		$("#navBtn div a").eq(num - 1).addClass("btn-outline");
		$("#navBtn div a").eq(num).removeClass("btn-outline");
		$("#navBtn div a").eq(num).removeClass("disabled");
		$(tid).hide();
		tid = "#tab-" + (num + 1);
		$(tid).show();
		webuploadInit = true;
	}
	$(window).scrollTop(0);

}
function upTab(num) {
	$("#navBtn div a").addClass("btn-outline");
	$("#navBtn .disabled").removeClass("btn-outline");
	$("#navBtn div a").eq(num - 2).removeClass("btn-outline");
	var tid = "#tab-" + num;
	$(tid).hide();
	tid = "#tab-" + (num - 1);
	$(tid).show();
	$(window).scrollTop(0);
}
// 富文本
function initSummernote() {
	description = CKEDITOR.replace('descriptionText');
	$('.summernote').summernote({
		lang: 'zh-CN'
	});
}

